import java.util.ArrayList;

public class PortfolioRepository {
	private static PortfolioRepository repository;
	private static ArrayList<Portfolio> portfolios = new ArrayList<Portfolio>();

	private PortfolioRepository() {
	}

	public static PortfolioRepository getPortfolioRepository() {
		if (repository == null) {
			repository = new PortfolioRepository();
		}
		return repository;
	}

	public void addPortfolio(Portfolio portfolio) {
		portfolios.add(portfolio);
	}

	public void deletePortfolio(Portfolio p) {
		portfolios.remove(p);
	}

	public Portfolio getPortfolioWithID(int portfolioID) {
		for (int i = 0; i < portfolios.size(); i++) {
			Portfolio portfolio = portfolios.get(i);

			if (portfolio.getPortfolioId() == portfolioID) {

				return portfolios.get(i);
			}
		}
		return null;
	}

	public Account getAccount(int accountId) {
		for (Portfolio portfolio : portfolios) {
			for (Account account : portfolio.getAccounts()) {
				if (account.getAccountId() == accountId) {
					return account;
				}
			}
		}
		return null;
	}

}
